package com.example.feed;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.x.uikit.service.IPageRouteService;
import com.x.uikit.page.XPage;
import com.x.uikit.router.XRoute;
import com.x.uikit.service.XService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FeedPageRouteList {
    public static String NATIVE_MULTI_CHANNEL = "native://native_multi_channel";
    public static String NATIVE_WEB = "native://web";
    public static String[] ROUTES = new String[]{
            NATIVE_MULTI_CHANNEL,
    };
    public static Map<String, IPageRouteService.PageLoader> sNavigatorMap = new ConcurrentHashMap<>();

    static {
        sNavigatorMap.put(NATIVE_MULTI_CHANNEL, new IPageRouteService.PageLoader() {
            @Override
            public XPage load(@NonNull XRoute route, @NonNull Context context) {
                MultiChannelDemo page = new MultiChannelDemo(context);
                page.setArgument(route.argument);
                return page;
            }
        });
    }

    public static String findPath(@NonNull XRoute route) {
        for (String path : FeedPageRouteList.ROUTES) {
            if (route.path.startsWith(path)) {
                return path;
            }
        }
        return null;
    }

    public static boolean hasPath(@NonNull XRoute route) {
        return findPath(route) != null;
    }

    public static void navigate(@NonNull XRoute route) {
        String path = findPath(route);
        if (TextUtils.isEmpty(path)) {
            return;
        }
        IPageRouteService.PageLoader handler = sNavigatorMap.get(path);
        IPageRouteService service = XService.get(IPageRouteService.class);
        if (service != null && handler != null) {
            service.navigate(route, handler);
        }
    }

}
